using Electric.Application.Auth;
using Electric.Application.Contracts.AppService.Todo;
using Electric.Application.Contracts.Dto.Identity.Todos;
using Electric.Application.Contracts.Dto.Identity.Users;
using Electric.Application.Contracts.Dto.Todos;
using Electric.Domain.Entitys.Todos;
using Microsoft.AspNetCore.Mvc;

namespace Electric.WebAPI.Controllers;

/// <summary>
/// todo
/// </summary>
[Route("api/[controller]")]
[ApiController]
// [EletricAuthorize]
public class TodosController : ControllerBase
{
    private ITodoAppService _todoAppService;

    /// <summary>
    /// injection
    /// </summary>
    /// <param name="todoAppService"></param>
    public TodosController(ITodoAppService todoAppService)
    {
        _todoAppService = todoAppService;
    }

    /// <summary>
    /// 添加todo
    /// </summary>
    /// <param name="todoCreateDto"></param>
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] TodoCreateDto todoCreateDto)
    {
        EleTodo eleTodo = await _todoAppService.InsertAsync(todoCreateDto);
        return Created(string.Empty, eleTodo);
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="todoPageRequestDto"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<IActionResult> Get([FromQuery] TodoPageRequestDto todoPageRequestDto)
    {
        //根据用户名搜索，分页返回用户列表
        TodoPageResponseDto todoPageResponseDto = await _todoAppService.GetPagedListAsync(todoPageRequestDto);
        return Ok(todoPageResponseDto);
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<IActionResult> Get(Guid id)
    {
        var todo = await _todoAppService.GetAsync(id);
        return Ok(todo);
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="id"></param>
    /// <param name="todoUpdateDto"></param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public async Task<IActionResult> Put(Guid id, [FromBody] TodoUpdateDto todoUpdateDto)
    {
        await _todoAppService.UpdateAsync(id, todoUpdateDto);
        return NoContent();
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _todoAppService.DeleteAsync(id);
        return NoContent();
    }
}

